window.Form = {};

//初始化验证
Form.initValid = function ($c) {
    //放到最前面，避免下面的组件初始化生成其他HTML
    $("input[input='true'],select[input='true'],textarea[input='true']", $c).each(function () {
        var $this = $(this);
        if (!$this.hasClass("easyui-combotree") && !$this.hasClass("easyui-combobox") && !$this.hasClass("easyui-selectx")) {
            $this.validatebox();
        }
    });
    if ($c.is("form")) {
        //保存原来的值，提交前作比较。@see easyui-form.js
        $c.data("DATA_OLD", $c.serialize());
    }
};
//提交前动作处理，进行验证
Form.onSubmit = function () {
    var $this = $(this);
    var pass = $this.form("validate");
    if (pass) {
        return true;
    }
    return false;
};

//取得表单参数
Form.getParams = function ($c) {
    var params = {};
    var array = $('input,select,textarea', $c).serializeArray();
    for (var i = 0; i < array.length; i++) {
        var pair = array[i];
        if (params[pair.name]) {
            params[pair.name] += "," + pair.value;
        } else {
            params[pair.name] = pair.value;
        }
    }
    return params;
};


//取得查询参数
Form.getQueryParams = function ($form) {
    var getEntry = function (entrys, name) {//取得键值对
        for (var i = 0; i < entrys.length; i++) {
            if (entrys[i]["name"] == name)
                return entrys[i];
        }
        return null;
    };
    var getOperate = function (inputs, name) {//取得操作符
        for (var i = 0; i < inputs.length; i++) {
            if (inputs[i]["name"] == (name + "_OP"))
                return inputs[i].value;
        }
        return "";
    };
    var getCustom = function ($form, name) {// 自定义查询条件标记
        var custom = $("#" + name, $form).attr("custom");
        return custom;
    };
    var entrys = [];
    var inputs = $('input,select,textarea', $form).serializeArray();
    for (var i = 0; i < inputs.length; i++) {
        var name = inputs[i].name;
        var value = (inputs[i].value || "").trim();
        var entry = getEntry(entrys, name);
        if (entry == null) {
            if (!name.endsWith("_OP")) {
                entry = {
                    "name": name,
                    "value": value,
                    "op": getOperate(inputs, name),
                    "custom": getCustom($form, name)
                };
                entrys.push(entry);
            }
        } else {
            entry["value"] += "," + value;
            //多个值的情况下默认为IN 15:IN 16 NOT IN
            entry["op"] = ["15", "16"].contains(entry["op"]) ? entry["op"] : "15";
        }
    }
    return $.toJSON(entrys);
};

//设置保存Service
Form.setSaveService = function ($form, saveService) {
    var state = $form.data('form');
    if (state) {
        var opts = state.options;
        saveService = saveService.replace(/\(.*\)/g, "").trim();
        opts.url = opts.url.replace(/service=.+?\&/g, "service=" + saveService + "&");
        //console.debug(opts.url);
    }
};

//取得表单当前状态
Form.getState = function ($c, others) {
    var state = [];
    var ids = others || [];
    var getText = function (input) {
        if (input.is("select") && input.val()) {
            return $("option:selected", input).text();
        } else if (input.data("combobox") || input.data("combotree")) {
            return input.combo("getText");
        }
        return input.val();
    };
    $('input,select,textarea', $c).each(function () {
        var $this = $(this);
        var input = $this.attr("input");
        if (input == "true") {
            var name = $this.attr("id");
            var label = $this.attr("label");
            var value = ($this.attr("value") || "").trim();
            if ($this.data("combobox") || $this.data("combotree")) {
                value = $this.combo("getValue");
            }
            state.push({"name": name, "label": label, "value": value, "text": getText($this)});
        }
    });
    for (var i = 0; i < ids.length; i++) {
        var input = $("#" + ids[i], $c);
        var name = input.attr("id");
        var label = input.attr("label") || "";
        var value = (input.val() || "").trim();
        state.push({"name": name, "label": label, "value": value, "text": getText(input)});
    }
    return state;
};

//提交表单
Form.submit = function (action, param, target) {
    var $form = $("#form-hidden-common");
    $form.attr("action", action || null);
    $form.attr("target", target || "_blank");
    var paramFields = $();
    param = param || {};
    try {
        for (var name in param) {
            var hidden = $('<input type="hidden" name="' + name + '">').val(param[name]).appendTo($form);
            paramFields = paramFields.add(hidden);
        }
        $form.submit();
    } finally {
        $form.attr('action', null);
        $form.attr('target', "_blank");
        paramFields.remove();
    }
};

window.Dialog = {};

//打开TAB
//Tab.open("用户信息查询","/admin/user.jsp",true);
//Tab.open({title:'用户信息',href:'/admin/user.jsp',closable:true,params:{name:'王小二',id:'512912237021252'}});
Dialog.open = function (title, url, params) {
    var $dialog = $("#dialog");
    $.get(url, params || {}, function (text) {
        $(".modal-body1", $dialog).remove();
        $(".modal-footer", $dialog).remove();
        $(".modal-title", $dialog).html(title);
        $(".modal-content").append($("<div class='modal-body1' style='padding: 15px'></div>").append(text));
        $(".modal-footer", $dialog).appendTo($(".modal-content", $dialog));
        $dialog.modal('show');
    });
};
//关闭TAB
Dialog.close = function () {
    var $dialog = $("#dialog");
    $dialog.modal('hide');
};
window.Layer = {};

Layer.open = function (url, param) {
    $.get(url, param, function (html) {
        layer.open({
            type: 1,
            title: false,
            closeBtn: 1,
            shadeClose: true,
            skin: 'layui-layer-demo',
            area: [($(window).width() - 100) + 'px', ($(window).height() - 40) + 'px'],
            content: html
        });
    });
};

